set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.29)
include(cmake/gcc-arm-none-eabi.cmake)
include(cmake/options/generate_config.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/common.cmake)
project(Furina LANGUAGES C CXX ASM)

# ========================= 手动设置目标项目 ========================
# 简简单单就好，之前用CMake用过头了，导致大面积暴雷，错误难以追踪。还是这种简简单单的指定方式好
set(TARGET_PROJECT_DIR Projects/driversDevelop)

# -------------- 创建可执行目标 --------------
add_executable(${PROJECT_NAME})
include_directories(Projects/common)
# 获取目标项目的配置
include(${TARGET_PROJECT_DIR}/config.cmake)
# ============================= 添加子目录 =============================
add_subdirectory(Drivers)

# ============================= Adapter =============================
add_library(gui INTERFACE)
target_include_directories(gui INTERFACE Adapter/GUI)
# 将头文件添加到接口库（可选，主要为了在IDE中显示）
#target_sources(my_headers INTERFACE FILE_SET HEADERS FILES ${HEADER_FILES})

# ============================= Utils =============================
add_library(utils INTERFACE)
target_include_directories(utils INTERFACE ${CMAKE_SOURCE_DIR})
# 将头文件添加到接口库（可选，主要为了在IDE中显示）
#target_sources(my_headers INTERFACE FILE_SET HEADERS FILES ${HEADER_FILES})

# ============================= BSP =============================
set(BSP_INC_DIRS BSP/inc BSP)
file(GLOB BSP_SRCS "BSP/src/*.cpp" "BSP/LT768/*.cpp")
add_library(bsp OBJECT)
target_sources(bsp PRIVATE ${BSP_SRCS})
target_include_directories(bsp PUBLIC ${BSP_INC_DIRS})
# 依赖
target_link_libraries(bsp PUBLIC STM32_DRIVERS hal)

# ============================= HAL =============================
set(HAL_INC_DIRS HAL/inc)
file(GLOB HAL_SRCS "HAL/src/*.cpp")
add_library(hal OBJECT)
target_sources(hal PRIVATE ${HAL_SRCS})
target_include_directories(hal PUBLIC ${HAL_INC_DIRS})
# 依赖
target_link_libraries(hal PUBLIC STM32_DRIVERS)

# ============================= Middleware =============================
set(MIDDLEWARE_DIR ${CMAKE_SOURCE_DIR}/Middleware)
set(THIRD_PARTY_DIR ${MIDDLEWARE_DIR}/Third_Party)
#----------- 第三方库 ------------
# ---- lvgl -----
set(LVGL_DIR ${THIRD_PARTY_DIR}/LVGL)
set(LVGL_INC_DIRS ${LVGL_DIR} ${LVGL_DIR}/lvgl/src)
file(GLOB_RECURSE LVGL_SRCS "${LVGL_DIR}/lvgl/src/*.c")
add_library(middleware_lvgl OBJECT EXCLUDE_FROM_ALL)
target_sources(middleware_lvgl PRIVATE ${LVGL_SRCS})
target_include_directories(middleware_lvgl PUBLIC ${LVGL_INC_DIRS})


# -------FreeRTOS-------
set(FREERTOS_DIR ${THIRD_PARTY_DIR}/FreeRTOS)
# FreeRTOS依赖于CMSIS，而CMSIS部分依赖于HAL，所以引入BSP目录
set(FREERTOS_INC_DIRS
        ${FREERTOS_DIR}
        ${FREERTOS_DIR}/Source/include
        ${FREERTOS_DIR}/Source/CMSIS_RTOS_V2
        ${FREERTOS_DIR}/Source/portable/GCC/ARM_CM4F
)
file(GLOB_RECURSE FREERTOS_SRCS
        "${FREERTOS_DIR}/Source/*.c"
        "${FREERTOS_DIR}/Source/CMSIS_RTOS_V2/cmsis_os2.c"
        "${FREERTOS_DIR}/Source/portable/GCC/ARM_CM4F/port.c"
)
add_library(freertos OBJECT)
target_sources(freertos PRIVATE ${FREERTOS_SRCS})
target_include_directories(freertos PUBLIC ${FREERTOS_INC_DIRS})
# 依赖
target_link_libraries(freertos PUBLIC STM32_DRIVERS)


# ------DSP库------
set(DSP_DIR ${MIDDLEWARE_DIR}/ST/ARM/DSP)
set(DSP_INC_DIR ${DSP_DIR}/Inc)
set(DSP_LIB_DIR ${DSP_DIR}/Lib)
add_library(dsp STATIC IMPORTED)
set_target_properties(dsp PROPERTIES
        IMPORTED_LOCATION "${DSP_LIB_DIR}/libarm_cortexM4lf_math.a"  # 静态库的完整路径
        INTERFACE_INCLUDE_DIRECTORIES ${DSP_INC_DIR}
)


# ETL可以实现状态机、简单任务调度、循环缓冲区等
add_subdirectory(${CMAKE_SOURCE_DIR}/Middleware/Third_Party/etl
        ${CMAKE_BINARY_DIR}/Middleware/Third_Party/etl_build)
target_link_libraries(${PROJECT_NAME} PRIVATE etl::etl)
target_link_libraries(hal PUBLIC etl::etl)

# --------- 包含子cmake ---------


# ============================= 目标项目文件 =============================
file(GLOB TARGET_PROJECT_SRCS "Projects/shared/*.cpp" "${TARGET_PROJECT_DIR}/core/*.cpp")
target_include_directories(${PROJECT_NAME} PRIVATE ${TARGET_PROJECT_DIR}/core Projects/shared)
target_sources(${PROJECT_NAME} PRIVATE ${TARGET_PROJECT_SRCS})
# 依赖
target_link_libraries(${PROJECT_NAME} PRIVATE STM32_DRIVERS hal bsp utils)




# ---------------------- 配置选项 -----------------------
if (ENABLE_FREERTOS)
    target_link_libraries(${PROJECT_NAME} PRIVATE freertos)
endif ()

if (ENABLE_GUI)
    target_link_libraries(${PROJECT_NAME} PRIVATE middleware_lvgl gui)
    target_include_directories(${PROJECT_NAME} PRIVATE ${TARGET_PROJECT_DIR}/ui)
    # UI资源文件
    file(GLOB UI_SRCS "${TARGET_PROJECT_DIR}/ui/*.c")
    target_sources(${PROJECT_NAME} PRIVATE ${UI_SRCS})
endif ()



# 导入Rust
if (ENABLE_RUST)
    add_dependencies(${PROJECT_NAME} rust_lib)
    target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/Rust/include")
    target_link_libraries(${PROJECT_NAME} PRIVATE
            "${CMAKE_SOURCE_DIR}/Rust/target/thumbv7em-none-eabihf/release/libembedded_rust.a"
    )
endif ()


if (ENABLE_AI)
    target_link_directories(${PROJECT_NAME} PRIVATE Middleware/ST/AI/Lib)
    target_include_directories(${PROJECT_NAME} PUBLIC Middleware/ST/AI/Inc)
    target_link_libraries(${PROJECT_NAME} PRIVATE :NetworkRuntime1000_CM4_GCC.a)
endif ()

target_link_libraries(${PROJECT_NAME} PUBLIC dsp)
# ============================= 后续处理 =============================
# 包含Rust编译的静态库
include(cmake/rust.cmake)

# 生成配置头文件（选项配置，见cmake/options/目录）
generate_config_header()